﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EngineLayer.Model
{
    public sealed class Logon : DBEntities
    {
        private User _user;
        private Registration _key;
        private MyApplication application;

        public MyApplication OpenApplication(Logon logon)
        {
            if (!CheckLogon(logon.user))
                throw new Exception(Resources.ErrorMessages.WrongLogin);

            if(!CheckKey(logon.key))
                throw new Exception(Resources.ErrorMessages.WrongRegistration);

            return application ?? (application = new MyApplication(this));
        }

        private bool CheckLogon(User user)
        {
            bool credentialsValidation = false;

            credentialsValidation = database.SystemUsers.Where(User => User.LoginID == user.UserName & User.Password == user.PassWord).Any();

            return credentialsValidation;


        }

        private bool CheckKey(Registration key)
        {
            return true;
        }

        public User user
        {
            get{return _user;}
            set{_user = value;}
        }

        public Registration key
        {
            get { return _key; }
            set { _key = value; }
        }
    }
}
